package com.woniu.intlet.web;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;

import com.woniu.command.purchasecommand.AddPurchaseCommand;
import com.woniu.command.purchasecommand.DeletePurchaseCommand;
import com.woniu.command.purchasecommand.UpdatePurchaseCommand;
import com.woniu.outlet.po.PurchasePo;
import com.woniu.service.PurchasePoService;
import com.woniu.uitl.ResponseResult;
import com.woniu.uitl.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "采购接口文档")
public class PurchaseController {

    @Autowired
    private PurchasePoService purchasePoService;


    @GetMapping("purchaseList")
    @ApiOperation("采购单列表")
    public ResponseResult<List<PurchasePo>> findAllPurchase(PurchasePo po){
        List<PurchasePo> allPurchase = purchasePoService.findAllPurchase(po);
        return new ResponseResult<List<PurchasePo>>(ResultCode.SUCCESS.getCode(),"OK",allPurchase);
    }

    @GetMapping("purchaseById/{id}")
    @ApiOperation("根据id查询采购单")
    public ResponseResult<PurchasePo> getPurchaseById(@PathVariable("id") Long id){
        PurchasePo purchasePo = purchasePoService.getPurchaeById(id);
        return new ResponseResult<PurchasePo>(purchasePo,"OK",ResultCode.SUCCESS.getCode());
    }


    @PostMapping("addPurchase")
    @ApiOperation(value = "添加采购单")
    @ApiOperationSupport(ignoreParameters = {"purchaseNo","id","status",
                                            "spare1","spare2","spare3",
                                            "auditTime","reviewer","fixTime",
                                            "fixPerson"})
    public ResponseResult<Map> addPurchase(@Validated PurchasePo purchasePo, BindingResult result, String name){
            if (result.hasErrors()){
                Map<String,Object> map = new HashMap<>();
                result.getFieldErrors().forEach((item) ->{
                    map.put(item.getField(),item.getDefaultMessage());
                });
                return new ResponseResult<Map>(ResultCode.PARAM_NOT_VALID.getCode(),"FAIL",map);
            }
        purchasePo.setCreatePerson(name);
        AddPurchaseCommand addPurchaseCommand = new AddPurchaseCommand();
        Integer excute = addPurchaseCommand.excute(purchasePo);
        if (excute == 1){
            return new ResponseResult<Map>(ResultCode.SUCCESS.getCode(), "OK", new HashMap());
        }else {
            return new ResponseResult<Map>(ResultCode.COMMON_FAIL.getCode(), "服务器异常", new HashMap());
        }

    }

    @PostMapping("updatePurchase")
    @ApiOperation(value = "修改采购单信息")
    @ApiOperationSupport(ignoreParameters = {"purchaseNo","status",
                                                "audit","auditTime",
                                                "reviewer","createTime",
                                                "createPerson","purchaseDetailPos"})
    public ResponseResult<Map> updatePurchase(@Validated()@RequestBody PurchasePo po,BindingResult result,String name){
        if (result.hasErrors()){
            Map<String,Object> map = new HashMap<>();
            result.getFieldErrors().forEach((item) ->{
                map.put(item.getField(),item.getDefaultMessage());
            });
            return new ResponseResult<Map>(ResultCode.PARAM_NOT_VALID.getCode(),"FAIL",map);
        }
        po.setFixPerson(name);
        UpdatePurchaseCommand updateCommand = new UpdatePurchaseCommand();
        Integer excute = updateCommand.excute(po);
        if (excute == 1){
            return new ResponseResult<Map>(ResultCode.SUCCESS.getCode(), "OK", new HashMap());
        }else {
            return new ResponseResult<Map>(ResultCode.COMMON_FAIL.getCode(), "服务器异常", new HashMap());
        }
    }

    @DeleteMapping ("deletePurchase")
    @ApiOperation(value = "删除未提交的采购单")
    public ResponseResult<String> deletePurchaseById(Long id){
        DeletePurchaseCommand deCommand = new DeletePurchaseCommand();
        String msg = deCommand.excute(id);
        return new ResponseResult<String>(ResultCode.SUCCESS.getCode(),msg);
    }

    @PutMapping("updatePurchase")
    @ApiOperation(value = "提交采购单")
    public ResponseResult<String> updatePurchaseStatusById(PurchasePo po,String name){
        UpdatePurchaseCommand upCommand = new UpdatePurchaseCommand();
        Integer flage = upCommand.excute(po,name);
        if (flage == 1){
            return new ResponseResult<String>(ResultCode.SUCCESS.getCode(),"提交成功");
        }else {
            return new ResponseResult<String>(ResultCode.COMMON_FAIL.getCode(),"提交失败");
        }
    }

    @PostMapping("updatePurStatus")
    @ApiOperation(value = "修改采购单状态")
    public ResponseResult<String> updatePurStatusById(Long id,String status){
        UpdatePurchaseCommand upCommand = new UpdatePurchaseCommand();
        Integer flage = upCommand.excute(id,status);
        if (flage == 1){
            return new ResponseResult<String>(ResultCode.SUCCESS.getCode(),"操作成功");
        }else {
            return new ResponseResult<String>(ResultCode.COMMON_FAIL.getCode(),"操作失败");
        }
    }


}
